package com.example.back.controller;

import com.example.back.model.*;
import com.example.back.model.Record;
import com.example.back.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class MainController {
    private static final Logger logger = LogManager.getLogger(MainController.class);

    @Resource
    AdminService adminService;

    @Resource
    RoomService roomService;

    @Resource
    UserService userService;

    @Resource
    BoardgameService boardgameService;

    @Resource
    ScenarioService scenarioService;

    @Resource
    PsGameService psGameService;

    @Resource
    SwitchGameService switchGameService;

    @Resource
    ProductService productService;

    @Resource
    OrderService orderService;

    @RequestMapping("")
    public String admin(HttpSession session) {
        if (session.getAttribute("adminAccount") != null) return "admin/admin";
        else return "admin/adminlogin";
    }

    @RequestMapping("adminPanel")
    public String adminPanel(Model model) {
        model.addAttribute("userCount", adminService.userCount());
        model.addAttribute("orderCount", adminService.orderCount());
        return "admin/panel";
    }

    @RequestMapping("selectAllEmployee")
    public String selectAllEmployee(Model model) {
        List<Employee> employees = adminService.selectAllEmployee();
        model.addAttribute("employees", employees);
        return "admin/ServiceAdmin/employees";
    }

    @RequestMapping("employeeSearch")
    public String employeeSearch(String employeeInfo, Model model) {
        List<Employee> employees;
        employees = adminService.selectByInformation(employeeInfo);
        model.addAttribute("employees", employees);
        return "admin/ServiceAdmin/employees";
    }

    @RequestMapping("selectAllRoom")
    public String selectAllRoom(Model model) {
        List<Room> rooms = roomService.findAllRoom();
        model.addAttribute("rooms", rooms);
        return "admin/ServiceAdmin/room";
    }

    @RequestMapping("roomSearch")
    public String roomSearch(String roomInfo, Model model) {
        List<Room> rooms;
        rooms = roomService.findByInformation(roomInfo);
        model.addAttribute("rooms", rooms);
        return "admin/ServiceAdmin/room";
    }

    @RequestMapping("findIntoRoom")
    public String findIntoRoom(int id, Model model){
        String name = roomService.findRoomName(id);
        List<User> users = roomService.findRoomUser(name);
        model.addAttribute("users", users);
        model.addAttribute("roomID", id);
        return "admin/ServiceAdmin/into_room";
    }

    @RequestMapping("leaveRoom")
    public String leaveRoom(int id, int roomID, Model model){
        String name = roomService.findRoomName(roomID);
        String account = roomService.findAccount(id);
        if(roomService.leaveRoom(account, name).equals("success")){
            logger.info("离开成功！");
        }
        else logger.info("离开失败！");
        List<User> users = roomService.findRoomUser(name);
        model.addAttribute("users", users);
        model.addAttribute("roomID", id);
        return "admin/ServiceAdmin/into_room";
    }

    @RequestMapping("roomType")
    public String roomType(String type, Model model) {
        List<Room> rooms;
        if (type.equals("all")) {
            rooms = roomService.findAllRoom();
        } else {
            rooms = roomService.roomType(type);
        }
        model.addAttribute("rooms", rooms);
        return "admin/ServiceAdmin/room";
    }

    @RequestMapping("roomState")
    public String roomState(int state, Model model) {
        List<Room> rooms;
        if (state == 77) {
            rooms = roomService.findAllRoom();
        } else {
            rooms = roomService.roomState(state);
        }
        model.addAttribute("rooms", rooms);
        return "admin/ServiceAdmin/room";
    }

    @RequestMapping("roomCount")
    public String roomCount(int roomC, Model model) {
        List<Room> rooms;
        if (roomC == 0) {
            rooms = roomService.findAllRoom();
        } else {
            rooms = roomService.roomCount(roomC);
        }
        model.addAttribute("rooms", rooms);
        return "admin/ServiceAdmin/room";
    }

    @RequestMapping("selectUsers")
    public String selectUsers(Model model) {
        List<User> users;
        users = userService.findAllUser();
        model.addAttribute("users", users);
        return "admin/ServiceAdmin/users";
    }

    @RequestMapping("userSearch")
    public String userSearch(String userInfo, Model model) {
        List<User> users;
        users = userService.findByInformation(userInfo);
        model.addAttribute("users", users);
        return "admin/ServiceAdmin/users";
    }

    @RequestMapping("selectAllBoardgames")
    public String selectAllBoardgames(Model model) {
        List<Boardgame> boardgames = boardgameService.findAllBoardgame();
        model.addAttribute("boardgames", boardgames);
        return "admin/GameAdmin/board";
    }

    @RequestMapping("selectBCount")
    public String selectBCount(int bCount, Model model) {
        List<Boardgame> boardgames = boardgameService.selectBCount(bCount);
        model.addAttribute("boardgames", boardgames);
        return "admin/GameAdmin/board";
    }

    @RequestMapping("selectBName")
    public String selectBName(String name, Model model) {
        List<Boardgame> boardgames = null;
        if (name.equals("")) {
            boardgames = boardgameService.findAllBoardgame();
        } else {
            boardgames = boardgameService.selectBName(name);
        }
        model.addAttribute("boardgames", boardgames);
        return "admin/GameAdmin/board";
    }

    @RequestMapping("selectAllScenario")
    public String selectAllScenario(Model model) {
        List<Scenario> scenarios = scenarioService.findAllScenario();
        model.addAttribute("scenarios", scenarios);
        return "admin/GameAdmin/kill";
    }

    @RequestMapping("selectKCount")
    public String selectKCount(int kCount, Model model) {
        List<Scenario> scenarios = scenarioService.selectKCount(kCount);
        model.addAttribute("scenarios", scenarios);
        return "admin/GameAdmin/kill";
    }

    @RequestMapping("selectKName")
    public String selectKName(String name, Model model) {
        List<Scenario> scenarios = null;
        if (name.equals("")) {
            scenarios = scenarioService.findAllScenario();
        } else {
            scenarios = scenarioService.selectKName(name);
        }
        model.addAttribute("scenarios", scenarios);
        return "admin/GameAdmin/kill";
    }

    //PS游戏管理
    @RequestMapping("selectAllPsGame")
    public String selectAllPsGame(Model model) {
        List<PsGame> psGames = psGameService.findAllPsGame();
        model.addAttribute("psGames", psGames);
        return "admin/GameAdmin/psGame";
    }

    @RequestMapping("searchPType")
    public String searchPType(String type, Model model) {
        List<PsGame> psGames = null;
        if (type.equals("all")) {
            psGames = psGameService.findAllPsGame();
        } else {
            psGames = psGameService.searchPType(type);
        }
        model.addAttribute("psGames", psGames);
        return "admin/GameAdmin/psGame";
    }

    @RequestMapping("selectPName")
    public String selectPName(String name, Model model) {
        List<PsGame> psGames = null;
        psGames = psGameService.findByInformation(name);
        model.addAttribute("psGames", psGames);
        return "admin/GameAdmin/psGame";
    }

    //Switch游戏管理
    @RequestMapping("selectAllSwitchGame")
    public String selectAllSwitchGame(Model model) {
        List<SwitchGame> switchGames = switchGameService.findAllSwitchGame();
        model.addAttribute("switchGames",switchGames);
        return "admin/GameAdmin/switchGame";
    }

    @RequestMapping("searchSType")
    public String searchSType(String type, Model model) {
        List<SwitchGame> switchGames = null;
        if (type.equals("all")) {
            switchGames = switchGameService.findAllSwitchGame();
        } else {
            switchGames = switchGameService.searchSType(type);
        }
        model.addAttribute("switchGames", switchGames);
        return "admin/GameAdmin/switchGame";
    }

    @RequestMapping("selectSName")
    public String selectName(String name, Model model) {
        List<SwitchGame> switchGames = null;
        switchGames = switchGameService.findByInformation(name);
        model.addAttribute("switchGames", switchGames);
        return "admin/GameAdmin/switchGame";
    }

    @RequestMapping("selectAllProduct")
    public String selectAllProduct(Model model) {
        List<Product> products = productService.findAllProduct();
        model.addAttribute("products", products);
        return "admin/SaleAdmin/goods";
    }

    @RequestMapping("selectProductType")
    public String selectProductType(String type, Model model) {
        List<Product> products = null;
        if (type.equals("all")) {
            products = productService.findAllProduct();
        } else {
            products = productService.findProductByType(type);
        }
        model.addAttribute("products", products);
        return "admin/SaleAdmin/goods";
    }

    @RequestMapping("selectGName")
    public String selectGName(String name, Model model) {
        List<Product> products = null;
        if (name.equals("")) {
            products = productService.findAllProduct();
        } else {
            products = productService.selectGName(name);
        }
        model.addAttribute("products", products);
        return "admin/SaleAdmin/goods";
    }

    @RequestMapping("selectAllThe_order")
    public String selectAllThe_order(Model model) {
        List<Order> orders = orderService.findAllOrder();
        model.addAttribute("orders", orders);
        return "admin/SaleAdmin/order";
    }

    @RequestMapping("findOrderProduct")
    public String selectOrderProduct(int id, Model model) {
        logger.info("id: " + id);
        int state = orderService.findOrderState(id);
        List<OrderProduct> products = orderService.findOrderProduct(id);
        model.addAttribute("products", products);
        model.addAttribute("orderID", id);
        model.addAttribute("state", state);
        return "admin/SaleAdmin/order_product";
    }

    @RequestMapping("selectOrderProductByType")
    public String selectProductType(int id, String type, Model model) {
        List<OrderProduct> products = null;
        int state = orderService.findOrderState(id);
        if (type.equals("all")) {
            products = orderService.findOrderProduct(id);
        } else {
            products = orderService.findOrderProductByType(id, type);
        }
        if(products == null)products = orderService.findOrderProduct(id);
        model.addAttribute("products", products);
        model.addAttribute("orderID", id);
        model.addAttribute("state", state);
        return "admin/SaleAdmin/order_product";
    }

    @RequestMapping("selectOrderProductByName")
    public String selectProductName(int id, String name, Model model) {
        List<OrderProduct> products = null;
        int state = orderService.findOrderState(id);
        if (name.equals("")) {
            products = orderService.findOrderProduct(id);
        } else {
            products = orderService.findOrderProductByName(id, name);
        }
        if(products == null)products = orderService.findOrderProduct(id);
        model.addAttribute("products", products);
        model.addAttribute("orderID", id);
        model.addAttribute("state", state);
        return "admin/SaleAdmin/order_product";
    }

    @RequestMapping("deleteOrderProduct")
    public String deleteOrderProduct(int id, int productID, int productCount, Model model) {
        List<OrderProduct> products = null;
        int state = orderService.findOrderState(id);
        logger.info("count: " + productCount);
        OrderProduct product = orderService.findTheProduct(id, productID);
        int count = product.getCount();
        double price = 0 - productCount * product.getPrice();
        if(productCount < count){
            orderService.updateProduct(id, productID, 0-productCount, price);
            logger.info("数量不够只更新");
        }
        else {
            orderService.removeProduct(id, productID);
            logger.info("数量够了就删除");
            price = 0 - count * product.getPrice();
        }
        orderService.addCost(id, price);
        products = orderService.findOrderProduct(id);
        model.addAttribute("products", products);
        model.addAttribute("orderID", id);
        model.addAttribute("state", state);
        return "admin/SaleAdmin/order_product";
    }

    @RequestMapping("findOrderByState")
    public String findOrderByState(int state, Model model) {
        List<Order> orders = null;
        if (state == 7) {
            orders = orderService.findAllOrder();
        } else {
            orders = orderService.findOrderByState(state);
        }
        model.addAttribute("orders", orders);
        return "admin/SaleAdmin/order";
    }

    @RequestMapping("findOrderByAccount")
    public String findOrderByAccount(String account, Model model) {
        List<Order> orders = null;
        if (account.equals("")) {
            orders = orderService.findAllOrder();
        } else {
            orders = orderService.findOrderByAccount(account);
        }
        model.addAttribute("orders", orders);
        return "admin/SaleAdmin/order";
    }

    @RequestMapping("findOrderByRoom")
    public String findOrderByRoom(String room, Model model) {
        List<Order> orders = null;
        if (room.equals("")) {
            orders = orderService.findAllOrder();
        } else {
            orders = orderService.findOrderByRoom(room);
        }
        model.addAttribute("orders", orders);
        return "admin/SaleAdmin/order";
    }

    @RequestMapping("selectAllInto_room")
    public String selectAllInto_room(Model model) {
        List<Record> records = adminService.selectAllInto_room();
        model.addAttribute("records", records);
        return "admin/SaleAdmin/record";
    }

    @RequestMapping("findInto_roomByRoom")
    public String findInto_roomByRoom(String name, Model model) {
        List<Record> records = null;
        if (name.equals("")) {
            records = adminService.selectAllInto_room();
        } else {
            records = adminService.findInto_roomByRoom(name);
        }
        model.addAttribute("records", records);
        return "admin/SaleAdmin/record";
    }

    @RequestMapping("findInto_roomByAccount")
    public String findInto_roomByAccount(String account, Model model) {
        List<Record> records = null;
        if (account.equals("")) {
            records = adminService.selectAllInto_room();
        } else {
            records = adminService.findInto_roomByAccount(account);
        }
        model.addAttribute("records", records);
        return "admin/SaleAdmin/record";
    }

    @RequestMapping("findInto_roomByFlag")
    public String findInto_roomByFlag(int flag, Model model) {
        List<Record> records = null;
        if (flag == 7) {
            records = adminService.selectAllInto_room();
        } else {
            records = adminService.findInto_roomByFlag(flag);
        }
        model.addAttribute("records", records);
        return "admin/SaleAdmin/record";
    }
}
